class GroovyScript {

    static getInputScript(redisKey, redisVariableNames, jmxVariableNames, isReusable = false) {
        if (!(redisKey && redisVariableNames && jmxVariableNames && redisVariableNames.length === jmxVariableNames.length)) {
            return "return";
        }
        return `import redis.clients.jedis.Jedis;
import groovy.json.JsonSlurper;

def redisHost = vars.get("redisHost");
def redisPort = Integer.parseInt(vars.get("redisPort"));
def redisDb = Integer.parseInt(vars.get("redisDb"));

def jedis = new Jedis(redisHost, redisPort);
try {
    jedis.connect();
    jedis.select(redisDb);

    def input = jedis.lpop("${redisKey}");
    ${isReusable ? `jedis.rpush("${redisKey}", input);` : ""}

    if (input == null) {
${jmxVariableNames.map(jmxVariableName => `        vars.put("${jmxVariableName}", "");`).join("\n")}
        return;
    } else {
        input = new JsonSlurper().parseText(input);
${jmxVariableNames.map((jmxVariableName, index) => `        vars.put("${jmxVariableName}", input.${redisVariableNames[index]});`).join("\n")}
    }

} catch (Exception e) {
    log.error("Redis操作失败: " + e.getMessage());
} finally {
	if (jedis.isConnected()) {
		jedis.close();
    }
}`;
    }

    static getOutputScript(redisKey, redisVariableNames, jmxVariableNames) {
        if (!(redisKey && redisVariableNames && jmxVariableNames && redisVariableNames.length === jmxVariableNames.length)) {
            return "return";
        }
        return `import redis.clients.jedis.Jedis;
import groovy.json.JsonOutput;

def redisHost = vars.get("redisHost");
def redisPort = Integer.parseInt(vars.get("redisPort"));
def redisDb = Integer.parseInt(vars.get("redisDb"));

def jedis = new Jedis(redisHost, redisPort);

try {
    jedis.connect();
    jedis.select(redisDb);

    if (${jmxVariableNames.map(variableName => `vars.get("${variableName}") != ""`).join(" && ")}) {
        def output = [${jmxVariableNames.map((jmxVariableName, index) => `"${redisVariableNames[index]}": vars.get("${jmxVariableName}")`).join(", ")}];
        output = JsonOutput.toJson(output);
        jedis.rpush("${redisKey}", output);
    }

} catch (Exception e) {
    log.error("Redis操作失败: " + e.getMessage());
} finally {
	if (jedis.isConnected()) {
        jedis.close();
    }
}`;
    }

    static getLargeValuesScript(testPlanName) {
        if (!testPlanName) {
            return "return";
        }
        return `import groovy.json.JsonSlurper;

def jmxPath = vars.get("jmxPath");
def jsonFile = new File(jmxPath + "/${testPlanName}_largeValue.json");
def variables = new JsonSlurper().parse(jsonFile);

variables.each { variable ->
    def name = variable.name;
    def value = variable.value;
    if (name != null && value != null) {
        vars.put(name, value);
    }
}`;
    }

    static getFinalizationAssertScript(jmxVariableNames = []) {
        if (!(jmxVariableNames && jmxVariableNames.length > 0)) {
            return "return";
        }
        return jmxVariableNames.map(jmxVariableName => `def ${jmxVariableName} = vars.get("${jmxVariableName}");
if (${jmxVariableName} == null || ${jmxVariableName}.isEmpty()) {
    AssertionResult.setFailure(true);
    AssertionResult.setFailureMessage("输出变量 [${jmxVariableName}] 为空");
} else {
    AssertionResult.setFailure(false);
    AssertionResult.setFailureMessage("");
}`).join("\n");
    }

}

module.exports = GroovyScript;